﻿using System.Collections;
using System;
using System.Collections.Generic;
using JZ.JiangSu.IRepository;
using JZ.JiangSu.Model.Entity;

namespace JZ.JiangSu.Repository
{
    public class CompanyInfoRepository : BaseRepository<CompanyInfo>,ICompanyInfoRepository
    {
        public int UpdateLogo(int id, string logoPath)
        {
            return Update(new List<int> { id }, new Dictionary<string, object>()
            {
                { "Logo", logoPath }, 
                { "UpdateTime",DateTime.Now }
            });
        }

        public int UpdateLicense(int uId, string licensePath)
        {
            return Update(new List<int> { uId }, new Dictionary<string, object>()
            {
                { "LicenseUrl", licensePath }, 
                { "UpdateTime",DateTime.Now },
                {"Reason",""}
            });
        }
        public int AlterTempleteId(int id, int templeteId)
        {
            return Update(new List<int> { id }, new Dictionary<string, object>()
            {
                {"TempleteId", templeteId}
            });
        }
        public int UpdateCompanyIsLicensedByUserIds(int[] userIds, bool isLicensed, string reason)
        {
            return Update(userIds, new Dictionary<string, object>()
            {
                {"IsLicensed", isLicensed ? 1 : 0},
                {"Reason", reason}
            });
        }
        /// <summary> 批量更新企业用户简历券余额 </summary>
        /// <param name="list">存放用户ID和余额</param>
        /// <returns></returns>
        public int UpdateCompanyInfoByIds(List<KeyValuePair<int, int>> list)
        {
            var arrId = new ArrayList();
            var sql = @"update CompanyInfo set DownloadableResumCount=DownloadableResumCount+
					case ID ";
            foreach (var t in list)
            {
                sql += " when " + t.Key + " then " + t.Value;
                arrId.Add(t.Key);
            }
            sql += " end where ID in(" + string.Join(",", arrId.ToArray()) + ")";
            var ht = new Hashtable { { "sql", sql } };
            return repository.Update("executeUpdateSql", ht);
            //return SqlHelperForCache.GetResultCount(ConnStr, sql);
        }
        /// <summary>
        /// 获取前topNumber的vip公司
        /// </summary>
        /// <param name="topNumber"></param>
        /// <returns></returns>
        public IList<CompanyInfo> GetTopVipCompany(int topNumber)
        {
            var ht = new Hashtable { { "top", topNumber } };
            return repository.QueryForList("GetTopVipCompany", ht);
        }

        public int GetCountCompanyName(string name)
        {
            var sql = @" IsBroker = 1 and IsLicensed = 0 and CreateTime > DATEADD(day,-3,GETDATE()) and name = '" + name + "'";
            var ht = new Hashtable { { "whereCondition", sql } };
            return repository.QueryForObject("CompanyInfoGetTotalCount", ht);
        }
    }
}
